Animating the Lorenz Attractor with Python

Edward Lorenz, the father of chaos theory, once described chaos as “when the present determines the future, but the approximate present does not approximately determine the future.”

Lorenz first discovered chaos by accident while developing a simple mathematical model of atmospheric convection, using three ordinary differential equations. He found that nearly indistinguishable initial conditions could produce completely divergent outcomes, rendering weather prediction impossible beyond a time horizon of about a fortnight.

In 1963, Lorenz developed a simple mathematical model for the way air moves around in the atmosphere, governed by the following equations:

$$\frac{dx}{dt} =\sigma \left ( y - x \right )$$$$\frac{dy}{dt} =x \left ( \rho - z \right ) - y$$$$\frac{dz}{dt} =xy - \beta z \tag{1}$$

Now known as the Lorenz System, this model demonstrates chaos at certain parameter values and its attractor is fractal. The animation we gone develop here depicts this system’s behavior over time in Python, using scipy to integrate the differential equations, matplotlib to draw the 3D plots, and pillow to create the animated GIF.

In three dimensions, these trajectories never overlap and the system never lands on the same point twice, due to its fractal geometry. We can also look at this attractor in two dimensions with matplotlib as I will show next

On the construction of the visualization of differente planes of the Lorenz attractor

Given that:

We need one more to set the initial values

Step through "time", calculating the partial derivatives at the current point and using them to estimate the next point

Now, proceeding to plot:

Now we plot two-dimensional cuts of the three-dimensional phase space, redefining iteration to make graphs even more beautiful

Animating the Lorenz Attractor

defining fonts to use for plots just beacuse we can

Define the initial system state, initial parameters and the time points to solve for, evenly spaced between the start and end times

Now for the Lorenz system:

Plot the system line simplificated in three dimensions

return a list in iteratively larger chunks, and handle them to facilitate future animation

Animation:

Create an animated gif of all the plots then display it inline

animated-lorenz-attractor.gif